
package io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import users.User;

/**
 * This class provides access to data for the peer review system.
 * 
 * @author Christoher M. Augg
 * 
 * @version March 4, 2014
 * 
 */
public class DataAccess {
  
  


  public static final int USER_ID = 0;
  public static final int USER_FIRSTNAME = 1;
  public static final int USER_LASTNAME = 2;
  public static final int USER_EMAIL = 3;
  public static final int USER_CONFERENCE_ID = 4;
  public static final int USER_CONFERENCE_TITLE = 5;
  public static final int USER_ROLE_ID = 6;
  public static final int USER_CONFERENCE_DESCRIPTION = 7;
  public static final int USER_ROLE = 8;

  
  /** A constant for the first name element in the XML file. */
  private static final String FIRSTNAME = "FirstName";
  
  /** A constant for the last name element in the XML file. */
  private static final String LASTNAME = "LastName";
  
  private static int next_id = 250;
  
  /** Object required for building document from XML file in JDOM2. */
  private SAXBuilder my_builder;

  /** File object to hold the XML file. */
  private File my_xmlfile;
  
  private Document my_doc;
  
  
  /** The constructor to instantiate the DataAccess object. */
  public DataAccess() {

    my_builder = new SAXBuilder();
    my_xmlfile = new File("data.xml");
    try {
      my_doc = (Document) my_builder.build(my_xmlfile);
    } catch (final IOException io) {
      System.out.println(io.getMessage());
    } catch (final JDOMException jdomex) {
      System.out.println(jdomex.getMessage());
    }

  }
  
  /**
   * This method allows you to retrieve the data for a specific user if they
   * are in the XML file. An empty list is returned if they do not exist in
   * the XML file.
   * 
   * @param the_fname The first name to be passed in for equality checking.
   *              
   * @param the_lname The last name to be passed in for equality checking.
   * 
   * @return An array list with all of the user's information if they exist
   *         and an empty list otherwise.
   */
  public List<String> getUserData(final String the_fname, final String the_lname) {

    /** An array list to hold the user's information. */
    final List<String> userdata = new ArrayList<String>();


    final Element rootnode = my_doc.getRootElement();
    final List<Element> list = rootnode.getChildren("UserID");

    for (int i = 0; i < list.size(); i++) {

      final Element node = (Element) list.get(i);

      if (node.getChildText(FIRSTNAME).equals(the_fname)
          && node.getChildText(LASTNAME).equals(the_lname)) {
          
        userdata.add(node.getAttributeValue("id"));
        userdata.add(node.getChildText(FIRSTNAME));
        userdata.add(node.getChildText(LASTNAME));
        userdata.add(node.getChildText("email"));
        userdata.add(node.getChildText("ConferenceID"));
        userdata.add(node.getChildText("ConferenceTitle"));
        userdata.add(node.getChildText("RoleID"));
        userdata.add(node.getChildText("ConferenceDescription"));
        userdata.add(node.getChildText("Role"));
      }
    }

    return userdata;
  }
  
  public List[] getAllUserData() {

    final Element rootnode = my_doc.getRootElement();
    final List<Element> list = rootnode.getChildren("UserID");
    
    final List[] result = new ArrayList[list.size()];

    for (int i = 0; i < list.size(); i++) {

      final Element node = (Element) list.get(i);
      final List<String> userdata = new ArrayList<String>();
          
      userdata.add(node.getAttributeValue("id"));
      userdata.add(node.getChildText(FIRSTNAME));
      userdata.add(node.getChildText(LASTNAME));
      userdata.add(node.getChildText("email"));
      userdata.add(node.getChildText("ConferenceID"));
      userdata.add(node.getChildText("ConferenceTitle"));
      userdata.add(node.getChildText("RoleID"));
      userdata.add(node.getChildText("ConferenceDescription"));
      userdata.add(node.getChildText("Role"));
      
      result[i] = userdata;
    }

    return result;
  }
  
  public void registerUserData(final User the_user) {
    
    final Element rootnode = my_doc.getRootElement();
    final Element usernode = new Element("UserID"); 
    rootnode.addContent(usernode);

    long id = next_id++;
    usernode.setAttribute("id", Long.toString(id));
    the_user.setID(id);
    usernode.addContent(new Element("id").setText(Long.toString(the_user.getID())));
    usernode.addContent(new Element(FIRSTNAME).setText(the_user.getFirstName()));
    usernode.addContent(new Element(LASTNAME).setText(the_user.getLastName()));
    usernode.addContent(new Element("email").setText(the_user.getEmail()));
    usernode.addContent(new Element("ConferenceID"));
    usernode.addContent(new Element("ConferenceTitle"));
    usernode.addContent(new Element("RoleID"));
    usernode.addContent(new Element("ConferenceDescription"));
    usernode.addContent(new Element("Role"));
    
    try {
      XMLOutputter out = new XMLOutputter();
      out.setFormat(Format.getPrettyFormat());
      
      out.output(my_doc, new FileWriter("data.xml"));
    } catch (final IOException e) {
      e.printStackTrace();
    }
    
  }

}
